﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data; 

namespace SoufunData
{
    public partial class ZConstructionForm : DevExpress.XtraEditors.XtraForm
    {

        public ZConstructionForm()
        {
            InitializeComponent();
            //dateEdit_start1.DateTime = DateTime.Now.AddDays(-60);
            //dateEdit_end1.DateTime = DateTime.Now;

            //dateEdit_start2.DateTime = DateTime.Now.AddDays(-60);
            //dateEdit_end2.DateTime = DateTime.Now;

            dateEdit_start1.Text = "不限制";
            dateEdit_start2.Text = "不限制";
            dateEdit_end1.Text = "不限制";
            dateEdit_end2.Text = "不限制";

         

            _dtResult = new DataTable();
            _dtResult.Columns.Add("总施工面积（㎡）");
            _dtResult.Columns.Add("总竣工面积（㎡）");
          
            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
        }


        private DataTable _dtResult;
        private readonly SqlClass _sqlClass = new SqlClass();

        private void AdvertisingForm_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
         
        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();

            if (dateEdit_start1.DateTime > DateTime.Parse("1000/01/01")
               && dateEdit_end1.DateTime > DateTime.Parse("1000/01/01"))
            {
                strWhere.Append("BeginDate between '");
                strWhere.Append(dateEdit_start1.DateTime);
                strWhere.Append("' and '");
                strWhere.Append(dateEdit_end1.DateTime);
                strWhere.Append("' ");
            }

            if (dateEdit_start2.DateTime > DateTime.Parse("1000/01/01")
              && dateEdit_end2.DateTime > DateTime.Parse("1000/01/01"))
            {
                if (strWhere.ToString()!="")
                {
                    strWhere.Append(" and ");
                }
                strWhere.Append("EndDate between '");
                strWhere.Append(dateEdit_start2.DateTime);
                strWhere.Append("' and '");
                strWhere.Append(dateEdit_end2.DateTime);
                strWhere.Append("' ");
            }
             
            if (textEdit_praValue.Text.Trim()!="")
            {
                if (strWhere.ToString()!="")
                {
                    strWhere.Append(" and ");
                }
                strWhere.Append(radioGroup1.Properties.Items[
                    radioGroup1.SelectedIndex].Value);                 
                strWhere.Append(" like '%");
                strWhere.Append(textEdit_praValue.Text);
                strWhere.Append("%'");
            }
            try
            {
                DataSet ds = _sqlClass.ZConstructionGetList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
                GetResultData(ds.Tables[0]);

                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                    btn_select.Enabled = true;
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } 
        }

        private void GetResultData(DataTable dt)
        {
            _dtResult.Clear();
            float totalSgmj = 0;
            float totalJgmj = 0;
            foreach (DataRow row in dt.Rows)
            {

                float sgmj = float.Parse(row["WorkArea"].ToString());
                float jgmj = float.Parse(row["OverArea"].ToString());
                totalSgmj += sgmj;
                totalJgmj += jgmj;
            }
            DataRow newDr = _dtResult.NewRow();
            newDr["总施工面积（㎡）"] = totalSgmj;
            newDr["总竣工面积（㎡）"] = totalJgmj;
            _dtResult.Rows.Add(newDr);
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = _dtResult;
                    gridView1.GroupPanelText = "共计" + _dtResult.Rows.Count + "条数据";
                }));
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            gridView2.GroupPanelText = "正在查询...";
            btn_select.Enabled = false;
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
         
        private void textEdit_proName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               btn_select_Click(null,null);
            }
        }

       
        private bool CheckDateEdit()
        {
            if (dateEdit_start1.DateTime<DateTime.Parse("1000/01/01")
                && dateEdit_end1.DateTime > DateTime.Parse("1000/01/01"))
            {
                XtraMessageBox.Show("请选择开工日期搜索范围的起始日期！");
                dateEdit_start1.Focus();
                return false;
            }
            if (dateEdit_start1.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_end1.DateTime < DateTime.Parse("1000/01/01"))
            {
                XtraMessageBox.Show("请选择开工日期搜索范围的结束日期！");
                dateEdit_end1.Focus();
                return false;
            }
            if (dateEdit_start2.DateTime < DateTime.Parse("1000/01/01")
            && dateEdit_end2.DateTime > DateTime.Parse("1000/01/01"))
            {
                XtraMessageBox.Show("请选择竣工日期搜索范围的起始日期！");
                dateEdit_start2.Focus();
                return false;
            }
            if (dateEdit_start2.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_end2.DateTime < DateTime.Parse("1000/01/01"))
            {
                XtraMessageBox.Show("请选择竣工日期搜索范围的结束日期！");
                dateEdit_end2.Focus();
                return false;
            }
            if (dateEdit_start1.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_end1.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_start1.DateTime>dateEdit_end1.DateTime)
            {
                XtraMessageBox.Show("您选择的开工日期搜索范围中的起始日期不能大于截止日期！");
                dateEdit_start1.Focus();
                return false;
            }
            if (dateEdit_start2.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_end2.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_start2.DateTime > dateEdit_end2.DateTime)
            {
                XtraMessageBox.Show("您选择的竣工日期搜索范围中的起始日期不能大于截止日期！");
                dateEdit_start2.Focus();
                return false;
            }
              

            return true;
        }



        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "施工数据";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "施工数据统计";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
    }
}